﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

namespace YetiManagement.App_Code
{
    public class Generator
    {

        private static string folderPath;
        private string fileName;
       


        public Generator()
        {

        }


        public static void generate()
        {
            //Querying with native sql            
            //using (yetiEntities1 ctx = new yetiEntities1())
            //{
            //    topics = ctx.ExecuteStoreQuery<Topic>("select * from Topic t, Pattern p where p.Content = '" + patternContent + "' and t.ID_topic = p.ID_topic", null).ToList();
            //}

            folderPath = "C:\\svn_repo\\bot-yeti\\generatedAIMLs\\";


            List<Subject> subjects = new List<Subject>();
            List<Topic> topics = new List<Topic>();
            List<Pattern> patterns = new List<Pattern>();
            List<Response> responses = new List<Response>();

          
          
            using (yetiEntities1 ctx = new yetiEntities1())
            {
                subjects = ctx.ExecuteStoreQuery<Subject>("SELECT * FROM Subject").ToList();
            }


            foreach (var subject in subjects)
            {
                XmlWriter writer;
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent = true;
                writerSettings.OmitXmlDeclaration = false;


                //create writer which will be writing to declared path - depended on subject name
                if(File.Exists(folderPath + subject.Name +".aiml") )                 
                {
                    writer= XmlWriter.Create(folderPath + subject.Name + "_copy.aiml", writerSettings);
                    
                }
                else
                {
                    writer = XmlWriter.Create(folderPath + subject.Name + ".aiml", writerSettings);
                
                }

                writer.WriteStartDocument();
                    writer.WriteStartElement("aiml");
                    using (yetiEntities1 ctx = new yetiEntities1())
                    {
                        topics = ctx.ExecuteStoreQuery<Topic>("SELECT * FROM Topic WHERE ID_subject = " + subject.ID_subject).ToList();
                    }


                    foreach (var topic in topics)
                    {
                        writer.WriteStartElement("topic");
                        writer.WriteAttributeString("name",topic.Name);
                        using (yetiEntities1 ctx = new yetiEntities1())
                        {
                            patterns = ctx.ExecuteStoreQuery<Pattern>("SELECT * FROM Pattern WHERE ID_topic = " + topic.ID_topic).ToList();
                        }
                        foreach (var pattern in patterns)
                        {
                            writer.WriteStartElement("category");
                                writer.WriteStartElement("pattern");
                                    writer.WriteString(pattern.Content);
                                writer.WriteEndElement();
                                using (yetiEntities1 ctx = new yetiEntities1())
                                {
                                    responses = ctx.ExecuteStoreQuery<Response>("SELECT * FROM Response WHERE ID_pattern = " + pattern.ID_pattern).ToList();
                                }

                                if(responses.Count() == 1)
                                {
                                    writer.WriteStartElement("template");
                                    writer.WriteString(responses.FirstOrDefault().Content);
                                    writer.WriteEndElement();
                                }
                                else
                                {
                                    writer.WriteStartElement("template");
                                        writer.WriteStartElement("random");
                                        foreach (var response in responses)
                                        {
                                            writer.WriteStartElement("li");
                                            writer.WriteString(response.Content);
                                            writer.WriteEndElement();
                                        }
                                        writer.WriteEndElement(); 
                                    writer.WriteEndElement();
                                }
                            
                            
                            writer.WriteEndElement();
                        }

                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }


        }

    }
}